package com.good.child.ui.utils;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;

/**
 * Create by xiaojie on 2024/8/25
 */
public class DateUtil {
    public static final String DATE_FORMART = "yyyy-MM-dd HH:mm:ss";

    public static String formatTimeAgo(String dateStr) {
        return formatTimeAgo(convertStringToDate(dateStr, DATE_FORMART));
    }

    public static String formatTimeAgo(Date date) {
        // 获取当前时间
        Date now = new Date();

        // 计算时间差（毫秒）
        long timeDifference = now.getTime() - date.getTime();

        // 转换为小时和天数
        long diffInHours = TimeUnit.MILLISECONDS.toHours(timeDifference);
        long diffInDays = TimeUnit.MILLISECONDS.toDays(timeDifference);

        if (diffInDays > 1) {
            // 超过一天，返回具体时间
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            return sdf.format(date);
        } else if (diffInDays == 1) {
            // 昨天
            return "昨天";
        } else if (diffInHours > 0) {
            // 小于一天但大于一小时
            return diffInHours + " 小时前";
        } else {
            // 小于一小时
            return "刚刚";
        }
    }

    /**
     * 将时间字符串转换成 Date 对象
     *
     * @param timeString 要转换的时间字符串
     * @param format     时间字符串的格式
     * @return 转换后的 Date 对象
     */
    public static Date convertStringToDate(String timeString, String format) {
        try {
            SimpleDateFormat dateFormat = new SimpleDateFormat(format);
            return dateFormat.parse(timeString);
        } catch (Exception e) {
            return new Date();
        }
    }

    public static  boolean checkIfDateIsToday(String dateStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date inputDate = sdf.parse(dateStr);
            Calendar calendarToday = Calendar.getInstance();
            Calendar calendarInput = Calendar.getInstance();
            calendarInput.setTime(inputDate);

            return calendarToday.get(Calendar.YEAR) == calendarInput.get(Calendar.YEAR)
                    && calendarToday.get(Calendar.MONTH) == calendarInput.get(Calendar.MONTH)
                    && calendarToday.get(Calendar.DAY_OF_MONTH) == calendarInput.get(Calendar.DAY_OF_MONTH);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public static String formatDate2Time(String originalDateStr,boolean onlyTime) {
        SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat outputFormat;
        if (onlyTime) {
            outputFormat = new SimpleDateFormat("HH:mm");
        } else {
            outputFormat = new SimpleDateFormat("M月d日 HH:mm");
        }
        try {
            Date date = inputFormat.parse(originalDateStr);
            String formattedTime = outputFormat.format(date);
            return formattedTime;
        } catch (Exception e) {
            e.printStackTrace();
        }
       return "";
    }

    public static boolean isToday(String dateString, String dateFormat) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(dateFormat, Locale.getDefault());
            Date date = sdf.parse(dateString);

            Calendar today = Calendar.getInstance();
            today.set(Calendar.HOUR_OF_DAY, 0);
            today.set(Calendar.MINUTE, 0);
            today.set(Calendar.SECOND, 0);
            today.set(Calendar.MILLISECOND, 0);

            Calendar dateCal = Calendar.getInstance();
            dateCal.setTime(date);
            dateCal.set(Calendar.HOUR_OF_DAY, 0);
            dateCal.set(Calendar.MINUTE, 0);
            dateCal.set(Calendar.SECOND, 0);
            dateCal.set(Calendar.MILLISECOND, 0);

            return today.getTimeInMillis() == dateCal.getTimeInMillis();
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
